package com.example.users.controller;

import com.example.users.service.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private CaptchaService captchaService;

    @GetMapping("/captcha")
    public ResponseEntity<?> getCaptcha() {
        CaptchaService.CaptchaResult captcha = captchaService.generateCaptcha();
        return ResponseEntity.ok().body(Map.of(
            "captchaId", captcha.getCaptchaId(),
            "captchaImage", captcha.getCaptchaImage()
        ));
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> body) {
        String captchaId = body.get("captchaId");
        String captchaCode = body.get("captchaCode");

        // 验证验证码
        if (!captchaService.validateCaptcha(captchaId, captchaCode)) {
            return ResponseEntity.badRequest().body(Map.of("error", "验证码错误"));
        }

        // TODO: 这里应该验证用户名和密码
        // 目前只是mock登录
        return ResponseEntity.ok().body(Map.of("token", "mock-jwt-token"));
    }

    @PostMapping("/logout")
    public ResponseEntity<?> logout() {
        return ResponseEntity.ok().body(Map.of("message", "logged out"));
    }
}


